﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sustainalytics.Reporting.Framework;
using Foxit.PDF.PageElements;
using Foxit.PDF;

namespace Sustainalytics.RiskRatings.Reporting
{
    public enum MeiImageColor
    {
        Colored,
        Black
    }

    public enum MeiImageSize
    {
        Small,
        Medium,
        Large
    }

    public abstract class PdfReportSectionGenerator : IPdfReportSectionGenerator
    {
        protected readonly PdfReport _report;
        protected readonly PdfReportSection _reportSection;
        private readonly List<OutlinePosition> outlinePositions;
        private readonly List<PageElementPosition> pageElementPositions;
        protected ReportDataSource _dataSource;
        public int OutlinePageCount = 1;
        public int HeaderHeight = 105;
        protected bool isCoreReport;

        public const float EmptyRowHeight = 10f;

        public List<OutlinePosition> OutlinePositions => outlinePositions;

        public List<PageElementPosition> PageElementPositions => pageElementPositions;

        protected PdfReportSectionGenerator(int index)
        {
            _report = new PdfReport();
            _report.PageSize = PageSize.A4;
            _report.PageOrientation = PageOrientation.Portrait;
            _report.PageMargins = 25f;
            _report.PageDimensions = new PageDimensions(_report.PageSize, _report.PageOrientation, _report.PageMargins);


            outlinePositions = new List<OutlinePosition>();
            pageElementPositions = new List<PageElementPosition>();


            _reportSection = new PdfReportSection
            {
                Id = Guid.NewGuid(),
                Name = "Sources Section",
                Index=index,
                PdfDocument = new byte[0],
                LastUpdate = DateTime.UtcNow,
                OutlinePositions = outlinePositions,
                PageElementPositions = pageElementPositions
            };
        }

        public PdfReportSection GeneratePdfReportSection(object objectDataSource)
        {
            _dataSource = objectDataSource as ReportDataSource;

            if (_dataSource == null || _dataSource.CompanyId == default(Guid))
            {
                throw new ArgumentException("ReportDataSource does not contain the necessary data for report generation!");
            }

            ComposeSectionContent();

            if (_report.Pages.Any())
            {
                _reportSection.PdfDocument = _report.Draw();
                _reportSection.OutlinePositions = OutlinePositions;
            }

            return _reportSection;
        }


        public abstract void ComposeSectionContent();
       

        protected static Table GetDefaultTable(float left, float top, float width, float height)
        {
            var table = new Table(left, top, width, height);
            table.Border.Width = 0.0f;
            table.Border.Color = Grayscale.Black;
            table.Border.LineStyle = LineStyle.None;
            table.BackgroundColor = RgbColor.White;
            table.CellSpacing = 0.0f;
            table.CellDefault.Align = TextAlign.Left;
            table.CellDefault.VAlign = VAlign.Top;
            table.CellDefault.Padding.Value = 0.0f;
            table.CellDefault.Border.Width = 0.0f;
            table.CellDefault.Border.LineStyle = LineStyle.None;
            table.CellDefault.Border.Color = RgbColor.White;

            return table;
        }
        
    }
}
